/* 
    业务逻辑
*/

const fs = require("fs");
const path = require("path");
const data = require("./data.json");

// 登录
exports.login = (req, res) => {
	// 获取表单数据
	let { email, username, password } = req.body;
	for(var i = 0; i < data.length; i++) {
		// 用户名或邮箱登录
		if((data[i].email == email && data[i].password == password) || (data[i].username == username && data[i].password == password)) {

			req.session.username = username;
			req.session.isAdmin = data[i].isAdmin;
			
			if(data[i].isFreeze) {
				res.send({
					status: -2,
					msg: "账号已被冻结"
				});
			} else {
				res.send({
					status: 0,
					msg: "登录成功!"
				});
			}
			return;
		}
	}
	res.send({
		status: -1,
		msg: "用户名或密码错误"
	})
};

// 注册
exports.register = (req, res) => {
	// 获取表单数据
	let info = req.body;
	info["isAdmin"] = false;
	info["isFreeze"] = false;
	let time = new Date().toLocaleString();
	info["time"] = time;

	if((Date.now() - req.session.timer)/1000 > 10) {
		res.send({
			status: -3,
			msg: "验证码过期,请重新发送..."
		})
		return;
	}

	let obj = {};
	// 先判断验证码 和 邮箱
	if(info.verify === req.session.verify || info.email === req.session.email) {
		for (var i = 0; i < data.length; i++) {
			// 在判断 用户名 和 邮箱 是否唯一
			if (data[i].username === info.username || data[i].email === info.email) {
				res.send({
					status: -1,
					msg: "用户名或邮箱与被使用..."
				});
				return;
			}
		}

		for (var key in info) {
			obj[key] = info[key];
		}
		data.push(obj);
		fs.writeFile(path.join(__dirname, "./data.json"), JSON.stringify(data, null, 4), err => {
			if (err) {
				res.send({
					status: -2,
					msg: "server ERROR"
				});
			} else {
				res.send({
					status: 0,
					msg: "注册成功"
				});
			}
		});
	} else {
		res.send({
			status: -1,
			msg: "验证码不正确,请重新获取..."
		})
	}
};

// 验证
exports.verify = (req, res) => {
	// 验证码
	let verify = Math.random()
		.toString()
		.substring(2, 6);
	// 表单数据
	let info = req.query;

	var timer = Date.now();

	req.session.verify = verify;
	req.session.email = info.email;
	req.session.timer = timer;

	// 验证码
	if (info.email) {
		res.send({
			status: 0,
			msg: "发送验证码成功!",
			data:{
				verify
			}
		});
	} else {
		res.send({
			status: -1,
			msg: "发送验证码失败"
		});
	}
};

// 退出登录
exports.logout = (req, res) => {
	req.session.username = '';

	res.send({
		status: 0,
		msg: "退出成功"
	});
};

// 获取用户信息
exports.getUser = (req, res) => {
	// console.log(req.session.isAdmin)
	if(req.session.username) {
		res.send({
			msg: "获取用户信息成功",
			status: 0,
			data: {
				username: req.session.username,
				isAdmin: req.session.isAdmin
			}
		})
	} else {
		res.send({
			msg: "获取用户信息失败",
			status: -1
		})
	}
};

// 找回密码
exports.findPassword = (req, res) => {
	let { email, verify } = req.body;

	if((Date.now() - req.session.timer)/1000 > 10) {
		res.send({
			status: -3,
			msg: "验证码过期,请重新发送..."
		})
		return;
	}

	if(email === req.session.email && verify === req.session.verify) {
		// 重置密码
		data.forEach((item) => {
			if (email == item.email) {
				for (let key in req.body) {
					item[key] = req.body[key];
				}
				return;
			}
		});
		fs.writeFile(path.join(__dirname, "./data.json"), JSON.stringify(data, null, 4), err => {
			if (err) {
				res.send({
					status: -2,
					msg: "server ERROR"
				});
			} else {
				res.send({
					status: 0,
					msg: "密码重置成功"
				});
			}
		});
	} else {
		res.send({
			status: -1,
			msg: "密码重置失败,请检查邮箱 验证码填写是否正确..."
		});
	}
};

// 管理员
exports.admin =(req, res) => {
	// console.log(req.session.username)
	// console.log(req.session.isAdmin)

	if(req.session.username && req.session.isAdmin) {
		res.send({
			status: 0,
			msg: "管理员权限"
		})
	} else {
		res.send({
			status: -1,
			msg: "没有管理员权限"
		})
	}
}

// 获取所有用户信息
exports.getAllUsers = (req, res) => {
	let arr = [];
	data.forEach(item => {
		arr.push(item)
	})
	if(arr) {
		res.send({
			status: 0,
			msg: "获取所有的用户信息",
			data: {
				userList: arr
			}
		})
	} else {
		res.send({
			status: -1,
			msg: "获取所有的用户信息失败"
		})
	}
}

// 账号冻结操作
exports.freeze = (req, res) => {
	let { email, isFreeze } = req.body
	if(isFreeze && email){
		res.send({
			status: -1,
			msg: "已被冻结"
		});
	} else {
		res.send({
			status: 0,
			msg: "解冻成功"
		});
	}
	// console.log(email, isFreeze)
	data.forEach(item => {
		if(item.email === email) {
			item.isFreeze = isFreeze
		}
	})
	fs.writeFile(path.join(__dirname, "./data.json"), JSON.stringify(data, null, 4), err => {
		if (err) {
			res.send({
				status: -2,
				msg: "server ERROR"
			});
		}
	});
}

// 删除
exports.removeUser =(req, res) => {
	let { email } = req.body;
	if(email) {
		res.send({
			status: 0,
			msg: "删除成功"
		});
	} else {
		res.send({
			status: -1,
			msg: "删除失败"
		});
	}
	data.forEach((item, index) => {
		if(item.email === email) {
			data.splice(index, 1)
		}
		return;
	})
	fs.writeFile(path.join(__dirname, "./data.json"), JSON.stringify(data, null, 4), err => {
		if (err) {
			res.send({
				status: -2,
				msg: "server ERROR"
			});
		}
	})
}

// 升级为管理员
exports.promote =(req, res) => {
	let { username } = req.body;
	data.forEach(item => {
		if (item.username === username) {
			item.isAdmin = true;
		}
		return;
	})
	fs.writeFile(path.join(__dirname, "./data.json"), JSON.stringify(data, null, 4), err => {
		if (err) {
			res.send({
				status: -2,
				msg: "server ERROR"
			});
		}
		res.send({
			status: 0,
			msg: "升级成功"
		});
	})

}